﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using LuaInterface;

namespace RxjhServer
{
	public class ScriptClass
	{
		public LuaFunction 怪物死亡事件;

		public LuaFunction 打开药品事件;

		public Dictionary<int, LuaFunction> 任务事件列表 = new Dictionary<int, LuaFunction>();

		public string 脚本目录 = "script";

		public Lua pLuaVM;

		public ScriptClass()
		{
			this.注册脚本API();
			this.脚本目录 = Application.StartupPath + "\\Script";
			this.GetUrlDirectory(this.脚本目录);
			Form1.WriteLine(2, "加载脚本完成");
			this.注册事件();
		}

		public void 注册事件()
		{
			this.打开药品事件 = this.pLuaVM.GetFunction("OpenDrugTrigGer");
			this.怪物死亡事件 = this.pLuaVM.GetFunction("DestroyMonster");
		}

		public void SetUrlFile(string Url)
		{
			string extension = Path.GetExtension(Url);
			if (extension == ".lua")
			{
				try
				{
					this.pLuaVM.DoFile(Url);
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex.Message);
					Form1.WriteLine(2, "加载Lua脚本出错" + ex.Message);
				}
			}
		}

		public void GetUrlDirectory(string url)
		{
			if (!Directory.Exists(url))
			{
				return;
			}
			string[] files = Directory.GetFiles(url);
			string[] array = files;
			for (int i = 0; i < array.Length; i++)
			{
				string urlFile = array[i];
				try
				{
					this.SetUrlFile(urlFile);
				}
				catch (Exception value)
				{
					Console.Write(value);
				}
			}
			this.GetUrlDirectoryS(url);
		}

		public void GetUrlDirectoryS(string url)
		{
			string[] directories = Directory.GetDirectories(url);
			string[] array = directories;
			for (int i = 0; i < array.Length; i++)
			{
				string url2 = array[i];
				this.GetUrlDirectory(url2);
			}
		}

		public void 注册脚本API()
		{
			try
			{
				this.pLuaVM = new Lua();
				this.pLuaVM.RegisterFunction("SendMissionMsg", this, base.GetType().GetMethod("任务提示数据发送"));
				this.pLuaVM.RegisterFunction("SendSysMsg", this, base.GetType().GetMethod("系统提示"));
				this.pLuaVM.RegisterFunction("SendKongfuMsg", this, base.GetType().GetMethod("学习技能提示"));
				this.pLuaVM.RegisterFunction("SendNoticeMsg", this, base.GetType().GetMethod("发送公告"));
				this.pLuaVM.RegisterFunction("AddDrugProp", this, base.GetType().GetMethod("增加药品属性"));
				this.pLuaVM.RegisterFunction("AddSE", this, base.GetType().GetMethod("增加善恶属性"));
				this.pLuaVM.RegisterFunction("AddQuest", this, base.GetType().GetMethod("设置任务数据"));
				this.pLuaVM.RegisterFunction("AddMission", this, base.GetType().GetMethod("设置任务数据"));
				this.pLuaVM.RegisterFunction("AddQigong", this, base.GetType().GetMethod("新学气功"));
				this.pLuaVM.RegisterFunction("AddSkill", this, base.GetType().GetMethod("学习技能"));
				this.pLuaVM.RegisterFunction("AddItme", this, base.GetType().GetMethod("增加物品Script"));
				this.pLuaVM.RegisterFunction("AddItmeProp", this, base.GetType().GetMethod("增加物品带属性"));
				this.pLuaVM.RegisterFunction("DelItme", this, base.GetType().GetMethod("删除物品"));
				this.pLuaVM.RegisterFunction("AddQuestItme", this, base.GetType().GetMethod("增加任务物品"));
				this.pLuaVM.RegisterFunction("AddTaoZhuang", this, base.GetType().GetMethod("增加套装Script"));
				this.pLuaVM.RegisterFunction("GetWorldItme", this, base.GetType().GetMethod("GetWorldItme"));
				this.pLuaVM.RegisterFunction("GetPlayer", this, base.GetType().GetMethod("GetPlayerThis"));
				this.pLuaVM.RegisterFunction("GetQuestLevel", this, base.GetType().GetMethod("Get任务阶段"));
				this.pLuaVM.RegisterFunction("GetPackage", this, base.GetType().GetMethod("得到包裹空位位置"));
				this.pLuaVM.RegisterFunction("GetPackages", this, base.GetType().GetMethod("得到包裹空位位置组"));
				this.pLuaVM.RegisterFunction("GetPakItme", this, base.GetType().GetMethod("得到包裹物品"));
				this.pLuaVM.RegisterFunction("GetPakItmeNum", this, base.GetType().GetMethod("得到包裹物品数量"));
				this.pLuaVM.RegisterFunction("GetQuestItme", this, base.GetType().GetMethod("得到任务物品"));
				this.pLuaVM.RegisterFunction("SetQuestLevel", this, base.GetType().GetMethod("设置任务阶段"));
				this.pLuaVM.RegisterFunction("SetPlayerTransfer", this, base.GetType().GetMethod("人物转职业"));
				this.pLuaVM.RegisterFunction("SetQigong", this, base.GetType().GetMethod("新学气功"));
				this.pLuaVM.RegisterFunction("SetPlayerLevel", this, base.GetType().GetMethod("设置玩家等级"));
				this.pLuaVM.RegisterFunction("SetPlayerVIP", this, base.GetType().GetMethod("设置人物会员"));
				this.pLuaVM.RegisterFunction("SetPlayerRxpiont", this, base.GetType().GetMethod("设置人物元宝"));
				this.pLuaVM.RegisterFunction("SetPlayerExp", this, base.GetType().GetMethod("设置人物经验值"));
				this.pLuaVM.RegisterFunction("SetPlayerJL", this, base.GetType().GetMethod("设置冲级奖励"));
				this.pLuaVM.RegisterFunction("ReSetPlayerLock", this, base.GetType().GetMethod("ReSetPlayerLevelLock"));
				this.pLuaVM.RegisterFunction("UpGongFu", this, base.GetType().GetMethod("更新武功和状态"));
				this.pLuaVM.RegisterFunction("UpMoney", this, base.GetType().GetMethod("更新金钱和负重"));
				this.pLuaVM.RegisterFunction("UpExp", this, base.GetType().GetMethod("更新经验和历练"));
				this.pLuaVM.RegisterFunction("UpYzbItme", this, base.GetType().GetMethod("初始话已装备物品"));
				this.pLuaVM.RegisterFunction("UpUseItme", this, base.GetType().GetMethod("物品使用"));
				this.pLuaVM.RegisterFunction("GetSTdata", this, base.GetType().GetMethod("判断师徒条件"));
				this.pLuaVM.RegisterFunction("UpSTdata", this, base.GetType().GetMethod("更新师徒数据"));
				this.pLuaVM.RegisterFunction("TEstMsg", this, base.GetType().GetMethod("TEstMsg"));
			}
			catch (Exception arg)
			{
				Form1.WriteLine(2, "注册脚本API出错-" + arg);
			}
		}

		public void TEstMsg(object aa)
		{
			Console.WriteLine(aa);
		}

		public void 学习技能提示(int UserWorldId)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.学习技能提示();
			}
		}

		public int 判断师徒条件(int UserWorldId, int 师徒等级, int 人物等级)
		{
			Players players;
			if (!World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				return -1;
			}
			if (players.Player_Level < 人物等级)
			{
				return -1;
			}
			if (players.师傅数据.STLEVEL != 师徒等级 || players.师傅数据.STYHD < 16)
			{
				return -1;
			}
			return 0;
		}

		public void 更新师徒数据(int UserWorldId, int 师徒等级)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.师傅数据.STYHD = 0;
				players.师傅数据.STLEVEL = 师徒等级;
				players.徒弟更新师徒系统();
				Players players2 = World.检查玩家name(players.师傅数据.STNAME);
				if (players2 != null)
				{
					for (int i = 0; i < 3; i++)
					{
						if (players2.徒弟数据[i].TID == players.师傅数据.TID)
						{
							players2.徒弟数据[i].STYHD = 0;
							players2.徒弟数据[i].STLEVEL = 4;
							players2.师傅更新师徒系统(i);
							return;
						}
					}
				}
			}
		}

		public void 设置人物元宝(int UserWorldId, int 元宝数, int 操作类型)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.查百宝阁元宝数();
				players.检察元宝数据(元宝数, 操作类型);
				players.保存元宝数据();
			}
		}

		public void 设置人物会员(int UserWorldId, int 会员时间)
		{
			Players players;
			if (会员时间 > 0 && World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				DateTime dateTime = players.FLD_VIPTIM;
				if (dateTime < DateTime.Now)
				{
					dateTime = DateTime.Now.AddDays((double)会员时间);
				}
				else
				{
					dateTime = dateTime.AddDays((double)会员时间);
				}
				players.FLD_VIP = 1;
				players.FLD_VIPTIM = dateTime;
				players.保存会员数据();
			}
		}

		public void 设置冲级奖励(int UserWorldId, int 奖励)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.Player_jllevel = 奖励;
				players.保存人物的数据();
			}
		}

		public void 设置人物经验值(int UserWorldId, int 经验)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.人物经验 += (long)经验;
				players.计算人物基本数据3();
				players.更新经验和历练();
			}
		}

		public void 设置玩家等级(int UserWorldId, int 等级, int 类型)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				if (类型 == 1)
				{
					if (players.Player_Level >= 等级)
					{
						return;
					}
					int num = 等级 - players.Player_Level;
					for (int i = 0; i < num; i++)
					{
						players.Player_Level++;
						if (players.Player_Level <= 34)
						{
							players.Player_Qigong_point++;
						}
						else
						{
							players.Player_Qigong_point += 2;
						}
					}
					players.计算人物基本数据3();
					players.更新HP_MP_SP();
					players.更新武功和状态();
					players.更新经验和历练();
					players.更新金钱和负重();
					return;
				}
				else
				{
					players.人物经验 = 0L;
					players.Player_Level = 等级;
					players.计算人物基本数据3();
					players.更新HP_MP_SP();
					players.更新武功和状态();
					players.更新经验和历练();
					players.更新金钱和负重();
				}
			}
		}

		public void 物品使用(int UserWorldId, int 位置, int 数量)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.物品使用(1,位置, 数量);
			}
		}

		public void 增加药品属性(int UserWorldId, int 攻击, int 防御, int 生命, int 元宝, int 武勋)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.增加属性(攻击, 防御, 生命, 元宝, 武勋);
			}
		}

		public void 增加善恶属性(int UserWorldId, int 善恶)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.增加善恶(善恶);
			}
		}

		public void 删除物品(int UserWorldId, int 位置, int 数量)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.减去物品(位置, 数量);
			}
		}

		public int 得到包裹物品(int UserWorldId, int 物品ID)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				for (int i = 0; i < 36; i++)
				{
					if (BitConverter.ToInt32(players.装备栏包裹[i].物品ID, 0) == 物品ID)
					{
						return i;
					}
				}
			}
			return -1;
		}

		public int 得到包裹物品数量(int UserWorldId, int 物品ID, int 数量)
		{
			int num = 0;
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				for (int i = 0; i < 36; i++)
				{
					if (BitConverter.ToInt32(players.装备栏包裹[i].物品ID, 0) == 物品ID)
					{
						num += players.装备栏包裹[i].Get物品数量;
					}
				}
				if (num >= 数量)
				{
					return 1;
				}
			}
			return -1;
		}

		public void 初始话已装备物品(int UserWorldId)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.初始话已装备物品();
			}
		}

		public void 更新经验和历练(int UserWorldId)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.更新经验和历练();
			}
		}

		public void 更新金钱和负重(int UserWorldId)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.更新金钱和负重();
			}
		}

		public void 更新武功和状态(int UserWorldId)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.更新武功和状态();
			}
		}

		public void 新学气功(int UserWorldId, int 气功位置)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.新学气功(气功位置);
			}
		}

		public void 人物转职业(int UserWorldId, int 人物正邪, int 转)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.人物转职业(人物正邪, 转);
			}
		}

		public int 得到包裹空位位置(int UserWorldId)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				return players.得到包裹空位位置();
			}
			return -1;
		}

		public List<int> 得到包裹空位位置组(int UserWorldId, int 数量)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				return players.得到包裹空位位置组(数量);
			}
			return new List<int>();
		}

		public int Get任务阶段(int UserWorldId, int 任务ID)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				return players.Get任务阶段(任务ID);
			}
			return 0;
		}

		public ItmeClass GetWorldItme(int 物品ID)
		{
			ItmeClass result;
			if (World.Itme.TryGetValue(物品ID, out result))
			{
				return result;
			}
			return null;
		}

		public Players GetPlayerThis(int UserWorldId)
		{
			Players result;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out result))
			{
				return result;
			}
			return null;
		}

		public void ReSetPlayerLevelLock(int UserWorldId)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.重置锁定状态();
			}
		}

		public void 增加物品Script(int UserWorldId, int 物品ID, int 空位, int 数量)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.增加物品Script(物品ID, 空位, 数量);
			}
		}

		public bool 增加套装Script(int UserWorldId, int 套装类别, int 套装等级)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.增加套装(套装类别, players.Player_Sex, players.Player_Job, players.Player_Zx, 套装等级);
				return true;
			}
			return false;
		}

		public bool 得到任务物品(int UserWorldId, int 物品ID, int 数量)
		{
			Players players;
			return World.allConnectedChars.TryGetValue(UserWorldId, out players) && players.得到任务物品(物品ID, 数量);
		}

		public void 增加任务物品(int UserWorldId, int 物品ID, int 数量)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.设置任务物品(物品ID, 数量);
			}
		}

		public void 增加物品带属性(int UserWorldId, int 物品ID, int 位置, int 数量, int 物品属性0, int 物品属性1, int 物品属性2, int 物品属性3, int 物品属性4, int 绑定)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.增加物品带属性(物品ID, 位置, 数量, 物品属性0, 物品属性1, 物品属性2, 物品属性3, 物品属性4, 绑定);
			}
		}

		public void 任务提示数据发送(int UserWorldId, int 任务ID, int 操作ID, int 任务阶段ID)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.任务提示数据发送(任务ID, 操作ID, 任务阶段ID);
			}
		}

		public int 设置任务阶段(int UserWorldId, int 任务ID, int 阶段)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				return players.设置任务阶段(任务ID, 阶段);
			}
			return 0;
		}

		public void 系统提示(int UserWorldId, string msg, int msgType, string name)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.系统提示(msg, msgType, name);
			}
		}

		public void 设置任务数据(int UserWorldId, int 任务ID, int 任务阶段ID)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.设置任务数据(任务ID, 任务阶段ID);
			}
		}

		public void 学习技能(int UserWorldId, int FLD_武功类型, int FLD_INDEX)
		{
			Players players;
			if (World.allConnectedChars.TryGetValue(UserWorldId, out players))
			{
				players.学习技能(FLD_武功类型, FLD_INDEX);
			}
		}

		public void 发送公告(string txt, int ggid)
		{
			try
			{
				foreach (Players current in World.allConnectedChars.Values)
				{
					if (current != null)
					{
						if (ggid == 0)
						{
							current.系统公告(txt);
						}
						else if (ggid == 1)
						{
							current.系统滚动公告(txt);
						}
						else if (ggid == 2)
						{
							current.系统提示(txt, 10, "系统信息");
						}
					}
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "发送公告出错：" + ex.Message);
			}
		}
	}
}
